package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.PageQuery;
import com.itheima.service.ClazzService;
import com.itheima.utils.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/clazzs")
@RequiredArgsConstructor
public class ClazzController {

    private final ClazzService clazzService;

    //分页查询
    @GetMapping
    public Result<PageBean> queryClazz(PageQuery pageQuery) {
        PageBean pageBean = clazzService.queryClazzByCondition(pageQuery);
        return Result.success(pageBean);
    }

    //添加班级
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz) {
        clazzService.addClazz(clazz);
        return Result.success("插入成功");
    }

    //修改班级
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        clazzService.updateClazz(clazz);
        return Result.success("修改成功");
    }


    //根据id查询
    @GetMapping("/{id}")
    public Result queryClazzById(@PathVariable("id") Integer id) {
        Clazz clazz = clazzService.queryClazzById(id);
        return Result.success(clazz);
    }

    @DeleteMapping("{id}")
    public Result deleteClazzById(@PathVariable("id") Integer id) {

        clazzService.deleteClazzById(id);

        return Result.success("删除成功");
    }

}
